/*
 * This is the GUI class. 
 */
package gui;

import model.ChatGUI2ModelCtrlr;
import ctrlr.ChatGUI2Ctrlr;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import model.ChatMsgs;

/**
 *
 * @author Nana
 */
public class ChatGUI implements ChatCtrlr2GUI {

    private static final boolean chatGUIVerbose = true;
    private ChatGUI2Ctrlr ctrlr;
    private ChatGUI2ModelCtrlr modelCtrlr;
    private MainWindow ihm;

    /**
     * Constructor of the GuI
     *
     * @param ctrlr The controller
     * @param guiCtrlr The GUI controller
     * @param userList The list of connected users
     * @param mess The messages to send
     * @throws IOException
     */
    public ChatGUI(ChatGUI2Ctrlr ctrlr, ChatGUI2ModelCtrlr guiCtrlr, DefaultListModel userList, ChatMsgs mess) throws IOException {
        this.ctrlr = ctrlr;
        this.modelCtrlr = guiCtrlr;
        ihm = new MainWindow(this, userList, mess);
    }

    /**
     * Used to follow the actions of the ChatSystem
     *
     * @param callingClass The class in which we are
     * @param callingFunction The called function
     * @param actionPerformed The action performed by the function
     */
    static void printDebug(String callingClass, String callingFunction, String actionPerformed) {
        if (ChatGUI.chatGUIVerbose) {
            System.out.println("In " + callingClass + " -> " + callingFunction);
            System.out.println("\t" + actionPerformed);
        }
    }

    /**
     * Connects the user to the system. [ From GUI to controller ]
     *
     * @param username The name of the local user
     */
    public void Connect(String username) {
        modelCtrlr.setUsername(username);
        ctrlr.performConnect(username);
        printDebug("ChatGUI", "performConnect", "performing connect");
    }

    /**
     * Disconnects the user to the system. [ From GUI to controller ]
     *
     * @param username The name of the local user
     */
    public void Disconnect(String username) {
        ctrlr.performDisconnect(username);
        printDebug("ChatGUI", "performConnect", "performing connect");
    }

    /**
     * Sends a message to remote users
     *
     * @param message The message to send
     * @param to The list of receivers
     */
    public void performSendTextMessage(String message, ArrayList<String> to) {
        printDebug("ChatGUI", "perfomSendTextMessage", "Sending a text message");
        ctrlr.performSendTextMessage(message, to);

    }

    /**
     * Sends a file proposal to a list of remote users
     *
     * @param filePath The path of the file to send
     * @param to The list of receivers
     */
    void performSendFileProposal(Path filePath, ArrayList<String> to) {
        ctrlr.performSendFileProposal(filePath, to);
    }

    /**
     * Creates a Pop UP to interract wih the local user when we receive a file
     * proposal
     *
     * @param fileName The name of the file to receive
     * @param size Its size
     * @param from The name of the sender
     * @param to The list of receivers
     * @return
     */
    @Override
    public boolean promptTransferPermission(String fileName, long size, String from, ArrayList<String> to) {
        int dialogResult = JOptionPane.showConfirmDialog(null, "Do you want to receive the file \n"
                + fileName + " (" + size + "octets" +  ")\nFrom :" + from + "\n", "Warning", JOptionPane.YES_NO_OPTION);
        if (dialogResult == JOptionPane.YES_OPTION) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Clean the list of connected users when the llocal user disconnects
     */
    public void emptyConnectedUserlist() {
        modelCtrlr.emptyConnectedUsersList();
    }
}
